//
//  SubmitReportViewController.m
//  Showing
//
//  Created by Ralf on 15/5/10.
//  Copyright (c) 2015年 com.next.starlooking. All rights reserved.
//

#import "SubmitReportViewController.h"
#import "AppDelegate.h"
#import "ReportManager.h"
#import "XLFormViewController+Helper.h"

@interface SubmitReportViewController ()
{
    AppDelegate   *appDelegate;
    ReportManager *reportManager;
}
@end

@implementation SubmitReportViewController

@synthesize report;

- (void)viewDidLoad
{
    [super viewDidLoad];
    reportManager = [ReportManager new];

    appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
    reportManager.parameterManager = appDelegate.parameterManager;

    [self initBackButton];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage imageNamed:@"Save-60.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(publishButtonOnTaps:) forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(0, 0, 30, 30);
    UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = menuButton;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (id)init
{
    self = [super init];

    if (self)
    {
        XLFormDescriptor        *form;
        XLFormSectionDescriptor *section;
        XLFormRowDescriptor     *row;

        form = [XLFormDescriptor formDescriptorWithTitle:@""];
        form.assignFirstResponderOnShow = YES;

        section = [XLFormSectionDescriptor formSection];

        [form addFormSection:section];

        row = [XLFormRowDescriptor formRowDescriptorWithTag:@"type" rowType:XLFormRowDescriptorTypeSelectorPush title:@"举报原因"];
        NSMutableArray      *options = [NSMutableArray new];
        XLFormOptionsObject *option  = [XLFormOptionsObject formOptionsObjectWithValue:@"色情低俗" displayText:@"色情低俗"];
        [options addObject:option];
        option = [XLFormOptionsObject formOptionsObjectWithValue:@"广告骚扰" displayText:@"广告骚扰"];
        [options addObject:option];
        option = [XLFormOptionsObject formOptionsObjectWithValue:@"诱导分享" displayText:@"诱导分享"];
        [options addObject:option];
        option = [XLFormOptionsObject formOptionsObjectWithValue:@"谣言" displayText:@"谣言"];
        [options addObject:option];
        option = [XLFormOptionsObject formOptionsObjectWithValue:@"政治敏感" displayText:@"政治敏感"];
        [options addObject:option];
        option = [XLFormOptionsObject formOptionsObjectWithValue:@"违法" displayText:@"违法（暴力恐怖、违禁品等）"];
        [options addObject:option];
        option = [XLFormOptionsObject formOptionsObjectWithValue:@"其他" displayText:@"其它（收集隐私信息等）"];
        [options addObject:option];
        option = [XLFormOptionsObject formOptionsObjectWithValue:@"侵权" displayText:@"侵权（诽谤、抄袭、冒用等）"];
        [options addObject:option];
        row.selectorOptions = options;
        row.required        = YES;
        [section addFormRow:row];

        row = [XLFormRowDescriptor formRowDescriptorWithTag:@"description" rowType:XLFormRowDescriptorTypeTextView];
        row.title    = @"补充说明";
        row.required = NO;
        [section addFormRow:row];

        self.form = form;
    }

    return self;
}

- (void)backButtonOnTap
{
    [self.view endEditing:YES];
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)publishButtonOnTaps:(id)sender
{
    if ([self.formValues[@"type"] isEqual:[NSNull null]])
    {

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请选择举报原因" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];

        return;
    }

    XLFormOptionsObject *type = self.formValues[@"type"];
    self.report[@"type"] = type.valueData;

    if ([self.formValues[@"description"] isEqual:[NSNull null]])
    {
    }
    else
    {
        self.report[@"description"] = self.formValues[@"description"];
    }

    ResultSet *rs = [reportManager submitReport:report];
    NSLog(@"%@", rs);
    if (rs.statusCode == 200)
    {
        [self alertSuccessMessage:@"你的举报已经提交审核，我们会尽快处理，审核结果将通过消息反馈给你。感谢你的支持！"];
    }

    [self.view endEditing:YES];
    [self.navigationController popViewControllerAnimated:YES];
}

@end
